package com.senven.role.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.common.entity.Role;
import com.senven.common.vo.CommonResult;
import com.senven.role.mapper.RoleMapper;
import com.senven.role.service.RoleSerive;
import com.senven.role.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Service
public class RoleServiceImpl implements RoleSerive {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 给其他的微服务的接口
     * @return
     */
    @Override
    public List<Role> findAllRole() {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        List<Role> roles = roleMapper.selectList(wrapper);
        return roles;
    }

    @Override
    public CommonResult findInfo(Integer currentPage, Integer pageSize, Role role) {
        Page<Role> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        Page<Role> roles = roleMapper.selectPage(page, wrapper);
        long total = page.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("roles",roles);
        if(roles != null){
            return new CommonResult(2000,"查询所有用户成功",map);
        }

        return new CommonResult(5000,"查询所有用户失败",null);
    }

    @Override
    public CommonResult updateRole(Role role){
        //先删除当前role
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult findRoleidByUserid(String userid) {
        return null;
    }


    //查询角色信息
    @Override
    public CommonResult selectRole(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        Page<Role> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Role> wrapper = new QueryWrapper<>();

        if (StringUtils.hasText(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }

        roleMapper.selectPage(page,wrapper);

        return new CommonResult(2000,"查询成功",page);
    }


    //添加角色
    @Override
    public CommonResult insert(Role role) {
        int insert = roleMapper.insert(role);
        return new CommonResult(2000,"添加成功",null);
    }

    //删除角色
    @Override
    public CommonResult delete(Integer roleid) {
        int i = roleMapper.deleteById(roleid);
        return new CommonResult(2000,"删除成功",null);
    }

    //修改角色
    @Override
    public CommonResult update(Role roleid) {
        int updateById = roleMapper.updateById(roleid);
        return new CommonResult(2000,"修改成功",null);
    }

}
